var _util = require('util/util.js');

var _cart_service = {
    check_cart : function(resolve, reject){
        console.log("check cart")
        _util.request({
            url : _util.getServerURL('cart/myCart'),
            method : 'GET',
            success : resolve,
            error : reject
        })
    },
    add_cart : function(itemId,resolve,reject){
        console.log("cart add");
        _util.request({
            url : _util.getServerURL('cart/myCart'),
            method : 'POST',
            data : {itemId},
            success : resolve,
            error : reject
        })
    },

    //改变cartItem的checked状态
    change_checked : function(itemId, checked, resolve, reject){
        _util.request({
            url : _util.getServerURL('cart/myCart/changeChecked'),
            method : 'POST',
            data : {itemId, checked},
            success : resolve,
            error : reject
        })
    },

    //全选或全不选
    check_all : function(checked, resolve, reject){
        _util.request({
            url : _util.getServerURL('cart/myCart/checkAll'),
            method : 'POST',
            data : {checked},
            success : resolve,
            error : reject
        })
    },

    //结算
    check_out : function(resolve, reject){
        _util.request({
            url : _util.getServerURL('cart/myCart/checkout'),
            method : 'POST',
            success : resolve,
            error : reject
        })
    },

    //删除选中的商品
    remove_checked: function(resolve, reject){
        _util.request({
            url : _util.getServerURL('cart/myCart/removeChecked'),
            method : 'POST',
            success : resolve,
            error : reject
        })
    },

    remove_cart : function(itemId, resolve, reject){
        console.log("cart remove");
        _util.request({
            url : _util.getServerURL('cart/myCart/cartItems'),
            method : 'DELETE',
            data : {itemId},
            success : resolve,
            error : reject
        })
    },
    update_quantity : function(itemId, quantity, resolve, reject){
        console.log("update quantity");
        _util.request({
            url : _util.getServerURL('cart/myCart/cartItems'),
            method : 'POST',
            data : {itemId, quantity},
            success : resolve,
            error : reject
        })
    }
}

module.exports = _cart_service;